home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / postgres / appgen-0.2-a / appgen-0 / AppGEN / src / java / clock.java < prev    next >
Encoding:
Java Source  |  1996-07-10  |  2.5 KB  |  109 lines

  1.  
  2. import java.applet.Applet;
  3. import java.awt.Graphics;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.util.Date;
  7. import java.lang.Math;
  8. import java.lang.Thread;
  9. import java.lang.System;
  10. import java.net.URL;
  11.  
  12.  
  13. public class clock extends Applet implements Runnable {
  14.     int AppletHeight, AppletWidth;
  15.     boolean die;
  16.     int a;
  17.  
  18.     public void init() {
  19.         Dimension d = size();        
  20.         AppletHeight = d.height;
  21.         AppletWidth = d.width;
  22.         }
  23.         
  24.     public void paint(Graphics g) {
  25.         painter(g,AppletHeight,AppletWidth);
  26.         }
  27.  
  28.  
  29.     public void tick() { 
  30.         repaint();
  31.         try {
  32.             Thread.sleep(1000);
  33.             } catch(InterruptedException e) ;
  34.         }
  35.  
  36.     public void start() {
  37.         die = false;
  38.         (new Thread(this)).start();
  39.         }
  40.  
  41.     public void run() {
  42.         while (!die) tick();
  43.         }
  44.  
  45.  
  46.     public void stop() {
  47.         die = true; 
  48.         }
  49.  
  50.     public void painter(Graphics g, int AppletHeight, int AppletWidth) {
  51.         
  52.         int a,p;
  53.         int cx,cy;
  54.         int ix, iy, jx, jy;
  55.         int hour, minute, sec;
  56.         double scale,fraction;
  57.         Date d = new Date();
  58.         Math m = null;
  59.         g.setColor(Color.white);
  60.         g.fillOval(2,2,AppletWidth-4,AppletHeight-4);
  61.         g.setColor(Color.blue);
  62.         cx = (int) AppletWidth / 2;
  63.         cy = (int) AppletHeight / 2;
  64.         p = (int) m.round(0.05 * AppletHeight);
  65.         for (a=1; a<p; a++) {
  66.             g.drawOval(a,a,AppletWidth-(2*a),AppletHeight-(2*a));
  67.             }
  68.         hour = d.getHours();
  69.         minute = d.getMinutes();
  70.         sec = d.getSeconds();
  71.         scale = (2 * 3.1415) / 12;
  72.             for (a=0; a<12; a++) {
  73.                 ix = (int) m.round( cx+(0.8*cx*m.cos(a*scale)) );
  74.                 iy = (int) m.round( cy+(0.8*cy*m.sin(a*scale)) );
  75.                 jx = (int) m.round( cx+(cx*m.cos(a*scale)) );
  76.                 jy = (int) m.round( cy+(cy*m.sin(a*scale)) );
  77.                 g.drawLine(ix,iy,jx,jy);
  78.                 
  79.             }
  80. // Draw Second Hand
  81.         scale = (2 * 3.1415) / 60;
  82.         ix = (int) m.round( cx );
  83.         iy = (int) m.round( cy );
  84.         jx = (int) m.round( cx+(0.8*cx*m.cos(sec*scale-(3.1415/2))) );
  85.         jy = (int) m.round( cy+(0.8*cy*m.sin(sec*scale-(3.1415/2))) );
  86.         g.setColor(Color.red);
  87.         g.drawLine(ix,iy,jx,jy);
  88.         g.setColor(Color.black);
  89. // Draw Minute Hand
  90.         scale = (2 * 3.1415) / 60;
  91.         ix = (int) m.round( cx );
  92.         iy = (int) m.round( cy );
  93.         jx = (int) m.round( cx+(0.8*cx*m.cos(minute*scale-(3.1415/2))) );
  94.         jy = (int) m.round( cy+(0.8*cy*m.sin(minute*scale-(3.1415/2))) );
  95.         g.drawLine(ix,iy,jx,jy);        
  96. // Draw Hour Hand
  97.         scale = (2 * 3.1415) / 12;
  98.         ix = (int) m.round( cx );
  99.         iy = (int) m.round( cy );
  100.         fraction = minute * 1.0;
  101.         fraction = fraction / 60.0;
  102.         jx = (int) m.round( cx+(0.5*cx*m.cos((hour + fraction)*scale-(3.1415/2))) );
  103.         jy = (int) m.round( cy+(0.5*cy*m.sin((hour + fraction)*scale-(3.1415/2))) );
  104.         g.drawLine(ix,iy,jx,jy);        
  105.         }
  106.  
  107. }
  108.  
  109.